package pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class AbstractCommandStackSelectionHandler extends AbstractSelectionHandler {

	// *** Attributes
	
	// *** Methods

	protected abstract Object execute(ExecutionEvent event, CommandStack commandStack, Object[] selection) throws ExecutionException;
	
	@Override
	public Object execute(ExecutionEvent event, Object[] selection) throws ExecutionException {
		
		IAdaptable adaptable = HandlerUtil.getActiveEditor(event);
		CommandStack commandStack = 
			(CommandStack) adaptable.getAdapter(CommandStack.class);
		if (commandStack == null) return null;
		
		/*
		 * So we have one node in selection array, and a command stack.
		 * We can perform the command execution.
		 */
		return execute(event, commandStack, selection);
	}

}
